package educar.controllers;


import javax.swing.JOptionPane;
import javax.swing.JTable;

import educar.gui.IView;
import educar.idioma.Locale;
import educar.models.Student;
import educar.models.studentNotFound;


/**
 * Permite borrar un alumno
 */
public class DeleteStudentController implements IController {
	private IView panel;
	private JTable table;
	
	/**
	 * Constructor
	 * @param table "JTable sobre la cual se busca el alumno a borrar"
	 */
	public DeleteStudentController(JTable table) {
		this.table = table;
	}

	public void process(String model){
		/*obtengo la fila seleccionada*/
		final int fila = table.getSelectedRow();
		if (fila == -1) {
			JOptionPane.showMessageDialog(null,Locale.FIRST_SELECT_ROW,Locale.NOT_ROW_SELECTED,JOptionPane.WARNING_MESSAGE);
		}
		else {
			try {
				Student old = Student.getStudentByDni(table.getModel().getValueAt(fila, 1).toString());
				int n = JOptionPane.showConfirmDialog(null,Locale.DELETE+" "+Locale.STUDENT+": "+old.getNombre()+" "+old.getApellido(),Locale.DELETE+" "+Locale.STUDENT,JOptionPane.YES_NO_OPTION);
				if (n == JOptionPane.OK_OPTION) {
						old.destroy();
						/*uso el present para refrescar la tabla*/
						panel.present(model);			
					}
			} catch (studentNotFound e) {
				JOptionPane.showMessageDialog(null,Locale.NOT_VALID_STUDENT,Locale.ERROR,JOptionPane.WARNING_MESSAGE);
			}
		}	
	}

	public void setView(IView view) {
		this.panel = view;
	}

}
